package com.ducquoc.examreviewdegreea1;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PhoArrayAdapter extends ArrayAdapter<PhoPojo> {

	Activity context = null;
	int layoutId;
	ArrayList<PhoPojo> arr = null;

	public PhoArrayAdapter(Activity context, int layoutId,
			ArrayList<PhoPojo> list) {
		super(context, layoutId, list);

		this.context = context;
		this.layoutId = layoutId;
		this.arr = list;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			convertView = inflater.inflate(layoutId, null);
		}

		PhoPojo pho = arr.get(position);

		ImageView icon = (ImageView) convertView.findViewById(R.id.ivPhoImage);
		TextView title = (TextView) convertView.findViewById(R.id.tvPhoTitle);
		TextView content = (TextView) convertView
				.findViewById(R.id.tvPhoContent);

		title.setText(pho.getTitle());
		content.setText(pho.getContent());

		String uri_icon = "drawable/" + pho.getIcon();
		int ImageResoure = convertView
				.getContext()
				.getResources()
				.getIdentifier(
						uri_icon,
						null,
						convertView.getContext().getApplicationContext()
								.getPackageName());
		Drawable image = convertView.getContext().getResources()
				.getDrawable(ImageResoure);
		icon.setImageDrawable(image);

		return convertView;
	}

}
